package wcl.controller;

import org.springframework.web.bind.annotation.*;
import wcl.Application;
import wcl.entity.AppointmentView;
import wcl.spring.ResponseResultBody;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author 武聪龙
 * @version 2021年11月12日
 */
@RestController
@RequestMapping("/appointment/char48")
@ResponseResultBody
public class AppointmentController {
    /**
     * 新增预约
     * @param view
     * @return
     */
    @PostMapping("/c")
    public int c(AppointmentView view) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime startTime = LocalTime.parse(view.getStart(),dtf);
        LocalTime endTime = LocalTime.parse(view.getEnd(),dtf);
        if (endTime.isBefore(startTime)) {
            System.out.println("结束时间不能小于开始时间");
            return 0;
        }
        if ((startTime.getMinute() != 0 && startTime.getMinute() != 30) || startTime.getSecond() != 0) {
            System.out.println("开始时间格式错误");
            return 0;
        }
        if ((endTime.getMinute() != 0 && endTime.getMinute() != 30) || endTime.getSecond() != 0) {
            System.out.println("结束时间格式错误");
            return 0;
        }
        int startIndex = startTime.getHour()*2+startTime.getMinute()/30;
        int endIndex = endTime.getHour()*2+endTime.getMinute()/30;
        byte[] bs = new byte[48];
        for (int i = 0; i < bs.length; i++) {
            if (i >= startIndex && i <= endIndex) {
                bs[i] = 49;
            } else {
                bs[i] = 48;
            }
        }
        if (conflict(date_string(Application.set),bs)) {
            view.setId(UUID.randomUUID().toString());
            Application.set.add(view);
            return 1;
        } else {
            return 0;
        }
    }

    @PutMapping("/u")
    public int u(AppointmentView view) {
        if (!Application.set.contains(view)) {
            System.out.println("该预约不存在");
            return 0;
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime startTime = LocalTime.parse(view.getStart(),dtf);
        LocalTime endTime = LocalTime.parse(view.getEnd(),dtf);
        if (endTime.isBefore(startTime)) {
            System.out.println("结束时间不能小于开始时间");
            return 0;
        }
        if ((startTime.getMinute() != 0 && startTime.getMinute() != 30) || startTime.getSecond() != 0) {
            System.out.println("开始时间格式错误");
            return 0;
        }
        if ((endTime.getMinute() != 0 && endTime.getMinute() != 30) || endTime.getSecond() != 0) {
            System.out.println("结束时间格式错误");
            return 0;
        }
        int startIndex = startTime.getHour()*2+startTime.getMinute()/30;
        int endIndex = endTime.getHour()*2+endTime.getMinute()/30;
        byte[] bs = new byte[48];
        for (int i = 0; i < bs.length; i++) {
            if (i >= startIndex && i <= endIndex) {
                bs[i] = 49;
            } else {
                bs[i] = 48;
            }
        }
        Set<AppointmentView> views = Application.set;
        views.remove(view);
        if (conflict(date_string(views),bs)) {
            Application.set.add(view);
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 查询所有预约
     * @return
     */
    @GetMapping("/r")
    public Set<AppointmentView> r() {
        System.out.println(date_string(Application.set));
        return Application.set;
    }

    /**
     * 将时间集合，转为48位二进制字符序列
     * @param set
     * @return
     */
    public String date_string(Set<AppointmentView> set) {
        byte[] times = new byte[48];
        ArrayList<Integer> indexs = new ArrayList<>();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        set.forEach(item -> {
            LocalTime startTime = LocalTime.parse(item.getStart(),dtf);
            LocalTime endTime = LocalTime.parse(item.getEnd(),dtf);
            int startIndex = startTime.getHour()*2+startTime.getMinute()/30;
            int endIndex = endTime.getHour()*2+endTime.getMinute()/30;
            for (int i = startIndex; i <= endIndex; i++) {
                indexs.add(i);
            }
        });
        for (int i = 0; i < times.length; i++) {
            if (indexs.contains(i)) {
                times[i] = 49;
            } else {
                times[i] = 48;
            }
        }
        return new String(times);
    }

    /**
     * 判断是否冲突
     * @param s 48位二进制字符序列
     * @param bs 需要新增的48位二进制字符序列的字节数组
     * @return true为不冲突，false为冲突
     */
    public boolean conflict(String s,byte[] bs) {
        if (s.length() != bs.length) {
            return false;
        }
        byte[] times = s.getBytes();
        for (int i = 0; i < times.length; i++) {
            if ((bs[i] + times[i]) > 97) {
                return false;
            }
        }
        return true;
    }

}
